package mediator

import (
	"fmt"
	"time"
)

// ChatRoom 聊天室作为中介者
type ChatRoom struct {
}

func (c *ChatRoom) ShowMsg(from *User, to, msg string) {
	fmt.Printf(
		"[%s]%s对%s说:%s\n",
		time.Now().Format("2006-01-02 15:04:05"),
		from.Name(),
		to,
		msg,
	)
}

func NewChatRoom() *ChatRoom {
	return &ChatRoom{}
}

type User struct {
	chatRoom *ChatRoom
	name     string
}

func NewUser(chatRoom *ChatRoom, name string) *User {
	return &User{chatRoom: chatRoom, name: name}
}

func (u *User) Name() string {
	return u.name
}

func (u *User) SendMsg(to, msg string) {
	u.chatRoom.ShowMsg(u, to, msg)
}
